import routerConfig from '../../router/config'

const permission = {
  state: {
    routers: routerConfig,
    addRouters: [],
    keepAlives: [],
    tempRouterFullPath: [],
    tempRouters: [],
    iframeKeepAlives: [],
    activatedFirst: undefined,
    tabRecord:[],//lcc添加标签记录
    spinningPrice:false,//页面内容的加载
    modalSpinningPrice:false,//弹窗的加载
    currentRemoveMenu:'',//当前删除标签页
    GXCAData:[],//签章结果
    editMode:'客户端模式',//编辑模式
    reviewProcessData:{},//审评环节数据--在提交组件用到
    APPSpinningData:{tipName:'正在加载中...',spinning:false},
    messagesNum:0,
    currentRouteName:'',//当前路由
  },
  mutations: {
    SET_CURRENTNAME:(state, data) => {
        state.currentRouteName = data
    },
    SET_ROUTERS: (state, data) => {
      state.addRouters = data
      state.routers = routerConfig.concat(data)
      //console.log('data路由有进来了===========吗',data)
      //console.log('data路由有进来了===========吗',routerConfig.concat(data))
    },
    SET_KEEPALIVES: (state, pageList) => {
      //console.log('路由有进来了===========吗？',pageList)
      state.tabRecord = pageList
      state.keepAlives = []
      state.iframeKeepAlives = []
      state.tempRouterFullPath = []
      pageList.forEach(item => {
        if (item.meta.keepAlive) {
          state.keepAlives.push(item.fullPath)
        }
        if(item.meta.tempRouter){
          state.tempRouterFullPath.push(item.meta.rootPath)
        }
        if(item.meta.isIframe){
          state.iframeKeepAlives.push(item)
        }
      })
      state.tempRouters = state.tempRouters.filter(item => state.tempRouterFullPath.indexOf(item.path) !== -1)
    },
    REMOVE_IFRAMEKEEPALIVES: (state, fullPath) => {
      state.iframeKeepAlives = state.iframeKeepAlives.filter(item => item.fullPath !== fullPath)
    },
    ADD_TEMPROUTER: (state, route) => {

      state.tempRouters.push(route)
      //console.log('真的有添加进来吗',state.tempRouters)
    },
    setActivatedFirst(state, activatedFirst) {
      state.activatedFirst = activatedFirst
    },
    SET_SPINNINGPRICE: (state, data) =>{
      state[data.stateKey] = data.key
      //console.log(data.key)
    },
    SET_REMOVEMENU:(state,data) =>{
      state.currentRemoveMenu = data
      //console.log('修改不成功',data)
    },
    SET_GXCADATA:(state,data) =>{
      state.GXCAData.push(data)
    },
    SET_GXCADATA_NULL:(state,data) =>{
      state.GXCAData = data
    },
    SET_EDITMODE:(state,data) =>{
      state.editMode = data
    },
    SET_PROCESSDATA:(state,data)=>{
      state.reviewProcessData = data
    },
    SET_GLOBALLOADING:(state,data)=>{
      state.APPSpinningData = {
        tipName:data.tipName?data.tipName:'正在加载中...',
        spinning:data.spinning?data.spinning:false,
      }
    },
    SET_MESSAGESNUM:(state,data) =>{
      //console.log('我在mutations里面也改变了',state.messagesNum,data)
      state.messagesNum = data
    },

  },
  actions: {
    UpdateCurrentRouteName({commit}, data) {
        return new Promise(resolve => {
            commit('SET_CURRENTNAME', data)
            resolve()
        })
    },
    // 动态添加主界面路由，需要缓存
    UpdateAppRouter({commit}, routes) {
      return new Promise(resolve => {
        let routelist = routes.constRoutes
        commit('SET_ROUTERS', routelist)
        resolve()
      })
    },
    UpdateSpinningPrice({commit}, data){
      return new Promise((resolve, reject) => {
        if(!data.key){
          commit('SET_SPINNINGPRICE', data)
          //console.log(1)
          resolve()
        }else if(data.key == 'true'){
          //console.log(2)
          commit('SET_SPINNINGPRICE', {key:true,stateKey:data.stateKey})
          resolve()
        }else{
          //console.log(3)
          commit('SET_SPINNINGPRICE', data)
          let timer = setInterval(()=>{
            //console.log(4)
            commit('SET_SPINNINGPRICE', {key:false,stateKey:data.stateKey})
            resolve()
            clearInterval(timer);
          },500)
        }


      })
    },
    //全局加载遮罩
    UpdateGlobalLoading({commit}, data){
      return new Promise(resolve => {
        commit('SET_GLOBALLOADING', data)
        resolve()
      })
    },
    UpdateReviewProcessData({commit}, data){
      return new Promise(resolve => {
        commit('SET_PROCESSDATA', data)
        resolve()
      })
    },
    //首页右上角消息角标数量
    UpdateMessagesNum({commit}, data){
      return new Promise((resolve, reject) => {
        //console.log('我真的在改变了')
        commit('SET_MESSAGESNUM', data)
        resolve()
      })
    }
  }
}

export default permission
